package javaSE.LibrarySystem.operation;

import javaSE.LibrarySystem.Book.Book;
import javaSE.LibrarySystem.Book.BookList;

public class DelOperation implements IOperation {
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        System.out.println("请输入你所要删除图书的名称：");
        String name = scanner.nextLine();
        int size = bookList.getUsedSize();
        int index = 0;//存储找到的下标\
        int i = 0;
        for (; i < size; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                index = i;
                break;
            }
        }
        if (i >= size) {
            System.out.println("没有你所要删除的书籍");
            return;
        } else {
            for (int j = index; j < size - 1; j++) {
                Book book = bookList.getBook(j+1);
                bookList.setBook(j, book);
            }
            bookList.setBook(size,null);
            bookList.setUsedSize(size-1);
            System.out.println("删除成功!");
        }
    }
}
